package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.dto.CommentAddParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.vo.CommentVo;
import cn.tedu._04weibo.pojo.vo.UserLoginVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "评论模块")
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("/insert")
    @Operation(summary = "插入评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam param , HttpSession session) {
        log.debug("插入评论,参数:{}",param);

        UserLoginVo loginVo = (UserLoginVo) session.getAttribute("user");
        if (loginVo == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(param,comment);
        comment.setUserId(loginVo.getId());
        comment.setCreateTime(new Date());

        int i = commentMapper.insert(comment);
        if (i > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    @GetMapping("/selectByWeiboId")
    @Operation(summary = "根据微博id查询评论")
    @ApiOperationSupport(order = 20)
    public JsonResult selectByWeiboId(@RequestParam Integer weiboId) {
        log.debug("根据微博id查询评论,参数:{}",weiboId);
        List<CommentVo> list = commentMapper.selectByWeiboId(weiboId);

        if (list.isEmpty()) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

        return JsonResult.ok(list);
    }


}
